package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("roles")
@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping
    public Result list(String name, String label, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult<Role> p = roleService.list(name, label, page, pageSize);
        return Result.success(p);
    }
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        roleService.deleteById(id);
        return Result.success();
    }
    @Log
    @PostMapping
    public Result add(@RequestBody Role role) {
        roleService.add(role);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Role role = roleService.getById(id);
        return Result.success(role);
    }
    @Log
    @PutMapping
    public Result upDate(@RequestBody Role role) {
        roleService.upDate(role);
        return Result.success();
    }

    @GetMapping("list")
    public Result listAll() {
        List<Role> list = roleService.listAll();
        return Result.success(list);
    }
}
